﻿using System;
using System.Data;
using System.Data.Sql;
using System.Windows.Forms;

namespace Operacional
{
    public partial class frmSql : Form
    {
        public frmSql()
        {
            InitializeComponent();
            cbDB.GotFocus += new EventHandler(cbDbGotFocus);
        }

        private void frmSql_Load(object sender, EventArgs e)
        {
            if (!Properties.Settings.Default.LocalChecked) chkLocal.Checked = false;
            else chkLocal.Checked = true;            
            getSqlServers();
        }

        private void btConnect_Click(object sender, EventArgs e)
        {
            if (chkLocal.Checked)
            {
                Properties.Settings.Default.conStr = "data source=" + cbServers.SelectedItem + "; Initial Catalog="
                    + cbDB.SelectedItem + "; integrated security=sspi; connection timeout=7";
            }
            else if ((!string.IsNullOrEmpty(txtUser.Text)) && (!string.IsNullOrEmpty(txtPass.Text)))
            {
                Properties.Settings.Default.conStr = "data source=" + cbServers.SelectedItem + "; Initial Catalog="
                    + cbDB.SelectedItem + "; user id=" + txtUser.Text + "; password=" + txtPass.Text + ";connection timeout=7";
            }
            Properties.Settings.Default.Save();
            frmEntrada.Conn.ConnectionString = Properties.Settings.Default.conStr;
            Close();
        }

        #region "Get Sql Servers"

        private void getSqlServers()
        {
            DataTable sqlServersTable = SqlDataSourceEnumerator.Instance.GetDataSources();

            foreach (DataRow rowOfData in sqlServersTable.Rows)
            {
                string serverName = rowOfData["ServerName"].ToString();
                string instanceName = rowOfData["InstanceName"].ToString();
                if (!instanceName.Equals(string.Empty)) serverName = serverName + "\\" + instanceName;
                cbServers.Items.Add(serverName);
            }
        }

        #endregion

        protected void cbDbGotFocus(object sender, EventArgs e)
        {
            try
            {
                frmEntrada.Conn.Close();
                if (chkLocal.Checked)
                    Properties.Settings.Default.conStr = "Data Source=" +cbServers.SelectedItem + ";Integrated Security=SSPI; Connection Timeout=7";
                else if ((!string.IsNullOrEmpty(txtUser.Text)) && (!string.IsNullOrEmpty(txtPass.Text)))
                    Properties.Settings.Default.conStr = "Data Source=" + cbServers.SelectedItem + "; User ID=" + txtUser.Text + "; Password=" + txtPass.Text + ";Connection Timeout=7";
                frmEntrada.Conn.ConnectionString = Properties.Settings.Default.conStr;
                getDatabases();
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        protected void getDatabases()
        {
            try
            {
                frmEntrada.Conn.Open();
                DataTable tblDatabases = frmEntrada.Conn.GetSchema("Databases");
                foreach (DataRow row in tblDatabases.Rows)
                    if (row["database_name"].ToString().IndexOf("AP") != -1)
                        cbDB.Items.Add(row["database_name"].ToString());
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
            frmEntrada.Conn.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        { Application.Exit(); }

        private void cbDbSelectedIndexChanged(object sender, EventArgs e)
        { if (cbDB.SelectedIndex != -1)   btConnect.Enabled = true; }
    }
}

